<?php

namespace App\Models;

class BondReturnLogs extends Base
{

    protected $table = 'bond_return_logs';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">未退还</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">已退还</span>',
    ];


    protected $fillable = [
        'id','user_id','user_name','bond','desc','link_name','link_phone','return_id','return_name','return_at','status','created_at','updated_at'
    ];

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('user_name','like','%' . $params['kwd'] . '%')
                        ->orwhere('return_name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('created_at',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
        $fields = [
            'id','user_name','bond','desc','link_name','link_phone','return_name','return_at',
            'status','created_at'
        ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->get();

        foreach ($lists as $k=>$v){
            if($v->attributes['status'] == 1){
                $v->attributes['status'] = '未退还';
            }else{
                $v->attributes['status'] = '已退还';
            }
        }
        return $lists;
    }
}
